<?php
namespace app\api\controller;

use app\model\RoleModel;
use think\Controller;
use think\Request;

class Role extends Controller
{

    public function add_post(Request $request){
        $name = trim($request->param('name'));

        if(empty($name)){
            return ['message'=>'角色名称不能为空','code'=>400];
        }

        $roleData = RoleModel::where('name','eq',$name)->find();

        if($roleData){
            return ['message'=>'角色名称已存在','code'=>'400'];
        }

        $roleModel = new RoleModel;
        $roleModel->name = $name;
        $roleModel->actions = '';
        $roleModel->createtime = time();
        $roleModel->save();

        if( $roleModel->id > 0 ){
            return ['message'=>'新增角色成功','code'=>'200'];

        }else{
            return ['message'=>'新增角色出错，请联系客服！','code'=>'400'];
        }


    }

    public function edit_post(Request $request){
        $id = trim($request->param('id'));
        $name = trim($request->param('name'));

        if(empty($id)){
            return ['message'=>'角色ID不能为空','code'=>400];
        }

        if(empty($name)){
            return ['message'=>'角色名称不能为空','code'=>400];
        }

        $existName = RoleModel::where('name', $name)->find();
        if($existName){
            return ['message'=>'角色名称已存在','code'=>'400'];
        }

        $roleData = RoleModel::where('id','eq',$id)->find();
        if(!$roleData){
            return ['message'=>'角色不存在','code'=>'400'];
        }

        $roleData->name = $name;
        $roleData->save();

        return ['message'=>'修改角色成功','code'=>'200'];

    }

    public function delete_post(Request $request){
        $id = trim($request->param('id'));

        if(empty($id)){
            return ['message'=>'角色ID不能为空','code'=>400];
        }

        $roleData = RoleModel::where('id','eq',$id)->find();
        if(!$roleData){
            return ['message'=>'角色不存在','code'=>'400'];
        }

        $roleData->delete();

        return ['message'=>'删除角色成功','code'=>'200'];

    }

    public function getlist_get(Request $request){
        $page = trim($request->param('page'));
        $pagesize = trim($request->param('pagesize'));
        $keyword = trim($request->param('keyword'));

        $page = empty($page) ? 1 : $page;
        $pagesize = empty($pagesize) ? 10 : $pagesize;
        
        $where = $keyword ? ":name like %".$keyword."%" : '1=1';

        $roleList = RoleModel::where($where)->page($page,$pagesize)->select();

        return ['message'=>'获取角色列表成功','code'=>'200','data'=>$roleList];

    }

    public function info_get(Request $request){
        $id = trim($request->param('id'));
        
        $roleData = RoleModel::where('id','eq',$id)->find();
        if(!$roleData){
            return ['message'=>'角色不存在','code'=>'400'];
        }

        return ['message'=>'获取角色成功','code'=>'200','data'=>$roleData];
    }

    public function actions_get(Request $request){
        $type = trim($request->param('type'));

        $type = $type == 1 ? 1 : 2;

        if($type == 1){
            $controllers = getControllers('../application/admin/controller');
        }else{
            $controllers = getControllers('../application/api/controller');
        }

        $actionsAll = [];
        foreach( $controllers as $key=>$value) {
            $actionsAll[$value] = getActions('app\api\controller' . '\\' . $value);
        }

        // var_dump($actionsAll);
    }

}
